﻿using System.Text;
using System;
namespace TSharp.Core.Util.Numeric
{
    /// <summary>
    /// 
    /// </summary>
    /// <author>
    /// tangjingbo
    /// </author>
    /// <remarks>
    /// tangj15 at 2012-3-16 9:59
    /// </remarks>
    public static class Codec
    {
        /// <summary>
        /// Xs to H.
        /// </summary>
        /// <param name="i">The i.</param>
        /// <param name="radix">The radix.</param>
        /// <returns></returns>
        public static string XToH(byte[] i, int radix)
        {


            return string.Empty;
        }

        /// <summary>
        /// Xs to H.
        /// </summary>
        /// <param name="i">The i.</param>
        /// <param name="radix">The radix.</param>
        /// <returns></returns>
        public static string XToH(long i, int radix)
        {
            if (radix < 2 || radix > 62)
                throw new OverflowException("基数需在[2-62]");
            bool flag = i < 0;
            if (flag) i = -i;
            StringBuilder ret = new StringBuilder();
            long j = 0;
            while (i >= radix)
            {
                j = i % radix;
                if (j <= 9)
                    ret.Insert(0, j);
                else if (j <= 35)
                    ret.Insert(0, Convert.ToChar(j - 10 + 'a'));
                else
                    ret.Insert(0, Convert.ToChar(j - 36 + 'A'));
                i = i / radix;
            }
            if (i <= 9)
                ret.Insert(0, i);
            else if (i <= 35)
                ret.Insert(0, Convert.ToChar(i - 10 + 'a'));
            else ret.Insert(0, Convert.ToChar(i - 36 + 'A'));

            if (flag) ret.Insert(0, '-');
            return ret.ToString();
        }
        /// <summary>
        /// Hs to X.
        /// </summary>
        /// <param name="hexString">The hex string.</param>
        /// <param name="radix">The radix.</param>
        /// <returns></returns>
        public static long HToX(string hexString, int radix)
        {
            if (radix < 2 || radix > 62)
                throw new OverflowException("基数需在[2-62]");
            if (string.IsNullOrEmpty(hexString))
                return 0;
            bool flag = hexString[0] == '-';
            int end = flag ? 1 : 0;
            long ret = 0L;
            int l = 0; 
            for (int i = hexString.Length - 1; i >= end; i--)
            {
                int v = 0;
                var pos = hexString[i];
                if (pos >= '0' && pos <= '9')
                {
                    v = Convert.ToInt32(pos - '0');
                }
                else if (pos >= 'a' && pos <= 'z')
                {
                    v = Convert.ToInt32(pos - 'a') + 10;
                }
                else if (pos >= 'A' && pos <= 'Z')
                {
                    v = Convert.ToInt32(pos - 'A') + 36;
                }
                else
                    v = char.MaxValue;
                if (v >= radix)
                    throw new OverflowException("字符内容超过进制限制");
                ret += v * Convert.ToInt64(Math.Pow(radix, l++));
            }
            return flag ? -ret : ret;
        }
    }
}